package pinyinJava.executors;

import pinyinJava.bean.DBean;
import pinyinJava.persistence.FilePer;
import pinyinJava.persistence.IDbPersistence;
import pinyinJava.persistence.IFilePersistence;
import pinyinJava.persistence.SqlitePer;

import java.io.File;
import java.io.IOException;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;

public class PyJson2DbExecutor {

    public void json2Db() throws SQLException, IOException {
        File dir = new File("pinyin-db");
        IDbPersistence dbPersistence = new SqlitePer();
        IFilePersistence filePersistence = new FilePer();
        for (File dir1 : dir.listFiles()) {
            if (!dir1.isDirectory()) continue;
            Connection connection = dbPersistence.open(dir1.getAbsolutePath() + File.separator + "PinYin" + dir1.getName() + ".db0");
            for (File txt : dir1.listFiles()) {
                if (!txt.getName().endsWith(".json")) continue;
                ArrayList<DBean> dBeans = filePersistence.read(txt.getAbsolutePath());
                dbPersistence.save(connection, txt.getName().substring(0, txt.getName().indexOf(".")), dBeans);
            }
            dbPersistence.close(connection);
        }
        System.out.println("done");
    }
}
